<template>
  <div :class="className"
       :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from "echarts"

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    headTitle: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "350px"
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Array,
      required: true
    },
    unit: {
      type: String,
      default: ""
    },
    color: {
      type: String,
      default: "#239DD2"
    }
  },
  data() {
    return {
      chart: null
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(
          this.headTitle,
          this.chartData,
        );
      }
    }
  },
  mounted() {
    window.addEventListener("resize", this.resizeHandler);
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      this.chart = echarts.init(this.$el);
      this.setOptions(
        this.headTitle,
        this.chartData,
      );
    },
    setOptions(title, chartData) {
      this.chart.setOption({
        title: {
          text: title,
          left: "center"
        },
        grid: {
          left: 25,
          right: 35,
          bottom: 20,
          top: 35,
          containLabel: true
        },
        xAxis: {
          data: [410, 435, 460, 485, 510, 535, 560, 585, 610, 645, 680, 705, 730, 760],
          boundaryGap: false, //falseX轴两边对齐
          //坐标轴刻度相关设置
          axisTick: {
            //是否显示坐标轴刻度
            show: false
          },
          name: "波长(nm)",
          nameLocation: "center",
          nameTextStyle: {
            fontWeight: "bold",
            padding: [10, 0, 0, 0]
          }
        },
        yAxis: {
          type: "value",
          name: "相对数值", nameTextStyle: {
            padding: [0, 0, 20, 0]
          },
          nameLocation: "center",
          min: 0,
          max: 1,
          interval: 0.1,
          boundaryGap: false,
          axisLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          splitLine: {
            show: true
          },
          /*axisLabel: {
            formatter: '{value} ' + unit
          }*/
          axisLabel: {
            formatter: function (value) {
              return (value)
            }
          }
        },
        //提示框组件
        tooltip: {
          //坐标轴触发，主要在柱状图，折线图等会使用类目轴的图表中使用
          trigger: "axis",
          //坐标轴指示器配置项
          axisPointer: {
            //显示X轴线条指示器
            type: "line",//shadow阴影效果，cross十字准星，line线条
            axis: 'x',
            //坐标轴指示器的文本标签
            label: {
              //文本标签中数值的小数点精度
              precision: 0
            }
          },
          formatter: '波长：{b0}<br />' + "相对数值" + '：{c0}',
          padding: [5, 10]
        },
        series: [
          {
            lineStyle: {
              //   shadowColor: this.color,
              // shadowOffsetX: 10,
              color: this.color,
              //   shadowOffsetY: 4,
              opacity: 1,
              //   shadowBlur: 10,
              width: 0
            },
            areaStyle: {
              opacity: 1,
              color: {
                type: 'linear',
                x: 0,
                y: 0,
                x2: 1,
                y2: 0,
                colorStops: [{
                  offset: 0, color: '#f201f6' // 0% 处的颜色
                },
                {
                  offset: 0.05, color: '#9c00ff'
                }, {
                  offset: 0.1, color: '#4200ff'
                },
                {
                  offset: 0.15, color: '#0110ff'
                }, {
                  offset: 0.2, color: '#0070fc'
                },
                {
                  offset: 0.25, color: '#00d2ff'
                }, {
                  offset: 0.3, color: '#00fe63'
                },
                {
                  offset: 0.35, color: '#29ff01'
                }, {
                  offset: 0.4, color: '#70fe02'
                },
                {
                  offset: 0.45, color: '#bafe02'
                }, {
                  offset: 0.5, color: '#fefb04'
                },
                {
                  offset: 0.55, color: '#feb103'
                }, {
                  offset: 0.6, color: '#ff5e00'
                },
                {
                  offset: 0.65, color: '#ff1a02'
                }, {
                  offset: 0.7, color: '#fe0000'
                }, {
                  offset: 1, color: '#fe0000'
                }
                ],
                global: false // 缺省为 false
              }
            },
            // itemStyle: {
            //   normal: {
            //     color: this.color,
            //     borderColor: this.color,
            //     lineStyle: {
            //       color: this.color,
            //       width: 2
            //     },
            //     label: {
            //       show: false,  //隐藏线点文字
            //       color: "#000"
            //     }
            //   }
            // },
            //name: title,
            smooth: true,     // 这个可以显示为平滑过渡的线条
            symbol: "circle", // 这个把节点的空心圆换成实心圆
            symbolSize: 1,    // 节点大小，数字随意，跟下面的线宽一样或少 1 像素
            type: "line",
            data: chartData,
            animationDuration: 2800,
            animationEasing: "cubicInOut"
          }
        ]
      });

    }
  }
};
</script>
